<template>
	<view>
		<view class="p-20">
			<image :src="imgUrl+detail.images" mode="widthFix" style="width: 100%;"></image>
			<view class="questions">
				<view class="flex space-around">
					<view class="text-center" @click="setIndex(0)">
						<view class="font-g">{{companyList.length}}家</view>
						<view class="font-24">参会企业</view>
					</view>
					<view class="text-center" @click="setIndex(1)">
						<view class="font-g">{{userList.length}}人</view>
						<view class="font-24">参会人数</view>
					</view>
				</view>
			</view>

			<view class="pt-20" v-for="(item, index) in companyList" v-if="selectIndex === 0"
				@click="$goRoute.goUrl('/pages/company/details?id='+item.companyId)">
				<view class="bg-white p-30 border-radius">
					<view class="flex items-center">
						<image v-if="item.logo" :src="item.logo" mode=""
							style="width: 70rpx;height: 70rpx;border-radius: 50%;">
						</image>
						<image v-if="!item.logo" src="@/static/images/company.png" mode=""
							style="width: 70rpx;height: 70rpx;border-radius: 50%;">
						</image>
						<view class=" ml-20">
							<view class="font-24">{{item.name}}</view>
							<view class="font-22 font-grey mt-10">{{item.scale}}人
							</view>
						</view>
					</view>
					<!-- <view class="mt-30" style="background-color: #F9FAFE;">
						<view class="flex space-between p-20">
							<view class="font-24">食品检测员</view>
							<view class="font-24 font-g">8-13k</view>
						</view>
						<view class="flex space-between p-20">
							<view class="font-24">销售精英（经销合伙人）</view>
							<view class="font-24 font-g">14-20k</view>
						</view>
					</view> -->
				</view>
			</view>

			<view class="pl-30 pr-30 pt-30" v-for="(item, index) in userList" :key="index" v-if="selectIndex === 1"
				@click="$goRoute.goUrl('/pages/lists/info?id='+item.applyuserId)">
				<view class="bg-white pl-30 pr-30 pt-20 pb-20 border-radius">
					<view class="flex space-between items-center mt-10">
						<view class="flex items-center" @click="$goRoute.goUrl('/pages/lists/info?id='+item.applyuserId)">
							<image src="../../static/images/avatar.png" mode=""
								style="width: 70rpx;height: 70rpx;border-radius: 50%;"></image>
							<view class=" ml-20">
								<view class="font-28">{{item.username}}</view>
								<view class="font-22 mt-10 font-grey">{{calculateAge(item.birthday)}}岁 |
									{{calculateAge(item.workTime)}}年 |
									{{item.zbApplyeducationList.length !== 0 ? item.zbApplyeducationList[0].education : '暂未填写学历'}}
									|
									{{item.zbApplyintentionList.length !== 0 ? item.zbApplyintentionList[0].expectPayLow + '-' + item.zbApplyintentionList[0].expectPayHigh : '暂未填写薪酬'}}
								</view>
							</view>
						</view>
						<view class="font-24 font-g border-g border-radius pt-10 pb-10 pl-20 pr-20">打招呼</view>
					</view>
					<view class="flex items-center mt-20" @click="$goRoute.goUrl('/pages/lists/info?id='+item.applyuserId)">
						<uni-icons custom-prefix="iconfont" type="icon-aixin_shixin" size="16" color="#46B4B1"></uni-icons>
						<view class="font-24 ml-20">期望：<text v-for="(itd, idx) in item.zbApplyintentionList"
								:key="idx">{{itd.expectPosition}}</text></view>
					</view>
					<view class="flex items-center mt-20" @click="$goRoute.goUrl('/pages/lists/info?id='+item.applyuserId)"
						v-if="item.zbApplyworkList.length !== 0">
						<uni-icons custom-prefix="iconfont" type="icon-gongzuoxinxi" size="16" color="#46B4B1"></uni-icons>
						<view class="font-24 ml-20">{{item.zbApplyworkList[0].companyName}} ·
							{{item.zbApplyworkList[0].position}}
						</view>
					</view>
				</view>
			</view>

			<view class="bottom-view">
				<view class="bottom-btn font-white bg-g" @click="submit">申请加入招聘会</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getInfo } from '../../api/apply_user.js';
	import { getInfo as getHr } from '../../api/hr_user.js';
import {
		queryById,
		queryZbJobfairCompanyByMainId,
		queryZbJobfairApplyuserByMainId,
		addFairUser,
		addFairCompany
	} from '../../api/job_fair.js';
import { BASE_URL } from '../../utils/constant.js';
	export default {
		data() {
			return {
				imgUrl: BASE_URL+ '/jeecg-boot/sys/common/static/',
				id: 0,
				detail: {},
				companyList: [],
				userList: [],
				selectIndex: 0
			};
		},
		onLoad(options) {
			this.id = options.id
			this.getDetail()
			this.getCompany()
			this.getUsers()
		},
		onShareAppMessage(res) {
			if (res.from === 'button') { // 来自页面内分享按钮
				console.log(res.target)
			}
			return {
				title: '河口人才招聘小程序',
				path: '/pages/fairs/details'
			}
		},
		methods: {
			calculateAge(birthdate) {
				var today = new Date();
				var birthDate = new Date(birthdate);
				var age = today.getFullYear() - birthDate.getFullYear();
				var monthDiff = today.getMonth() - birthDate.getMonth();
			
				if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
					age--;
				}
			
				return age;
			},
			setIndex(index) {
				this.selectIndex = index
			},
			getCompany() {
				queryZbJobfairCompanyByMainId({
					id: this.id,
					pageSize: 1000
				}).then(res => {
					this.companyList = res.result
				})
			},
			getUsers() {
				queryZbJobfairApplyuserByMainId({
					id: this.id,
					pageSize: 1000
				}).then(res => {
					this.userList = res.result
				})
			},
			getDetail() {
				queryById({
					id: this.id
				}).then(res => {
					this.detail = res.result
				})
			},
			submit() {
				const _this = this
				this.$tips.showModal('是否申请加入招聘会？', () => {
					// _this.$tips.showConfirmModal('提交成功，请等待审核', () => {})
					// 判断角色
					const userInfo = uni.getStorageSync('userInfo')
					if (userInfo.type === 0) {
						// 求职用户报名，获取求职用户id
						getInfo().then(res => {
							if (res.code === 200) {
								addFairUser({
									applyuserId: res.result.id,
									jobfairId: this.id
								}).then(res => {
									if (res.code === 200) {
										_this.$tips.showConfirmModal('提交成功，请等待审核', () => {
											uni.navigateBack()
										})
									} else {
										_this.$tips.showConfirmModal('提交失败', () => {})
									}
								})
							} else {
								_this.$tips.showConfirmModal(res.message, () => {})
							}
							
						})
						
					} else {
						// 公司报名
						getHr().then(res => {
							if (res.code === 200) {
								addFairCompany({
									companyId: res.result.companyId,
									jobfairId: this.id
								}).then(res => {
									if (res.code === 200) {
										_this.$tips.showConfirmModal('提交成功，请等待审核', () => {
											uni.navigateBack()
										})
									} else {
										_this.$tips.showConfirmModal('提交失败', () => {})
									}
								})
							} else {
								_this.$tips.showConfirmModal(res.message, () => {})
							}
							
						})
					}
				})
			}
		}
	}
</script>

<style lang="less">
	page {
		background-color: #f6f6f6;
		padding-bottom: 200rpx;
	}

	.bottom-view {
		width: 100vw;
		padding: 40rpx 0;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 99999;
		border-top: 1px solid #efefef;

		.bottom-btn {
			width: 80vw;
			margin: auto;
			text-align: center;
			border-radius: 50rpx;
			padding: 20rpx 0;
		}
	}

	.questions {
		background-color: #fff;
		padding: 30rpx 0;
		position: relative;
		border-radius: 20rpx;
		margin-top: -20rpx;
	}
</style>